package com.deng.common.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.dao.IFunction;
import com.deng.common.vo.DataTableVo;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.EmployeeVo;
import com.deng.common.vo.FunctionVo;
import com.deng.performance.dao.impl.DispenseEmployeeImpl;
import com.deng.performance.dao.impl.OrganizationFirstBonusImpl;
import com.deng.performance.vo.OrganizationFirstBonusVo;
import com.deng.util.DateTools;
import com.deng.util.GlobalProperties;

public class FunctionImpl extends SQLHelper implements IFunction {

	public FunctionVo getFunctionByName(String function_name) {
		// TODO Auto-generated method stub
		return null;
	}


	public FunctionVo getFunctionById(int id) {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from APP_FUNCTION where id = "+id);
			if(rs.next()){
				FunctionVo vo = new FunctionVo();
				vo.function_name = rs.getString("function_name");
				vo.function_detail = rs.getString("function_detail");
				freeConnection(conn);
				return vo;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
		
		
	}

//	public String convertToFunctionDetail(String name) {// chinese name ------>    function 
//		try{
//			if(name != null){
//				String detail = name;
//				Connection conn = getConnect();
//				ResultSet rs = queryData(conn,"select * from APP_FUNCTION");
//				while(rs.next()){
//					String function_detail = rs.getString("function_detail");
//					String function_name = rs.getString("function_name");
//					function_name = function_name.replaceAll("\\+", "\\\\+");
//					function_name = function_name.replaceAll("\\*", "\\\\*");
//					function_name = function_name.replaceAll("\\/", "\\\\/");
//					function_name = function_name.replaceAll("\\-", "\\\\-");
//					function_name = function_name.replaceAll("\\(", "\\\\(");
//					function_name = function_name.replaceAll("\\)", "\\\\)");
//					detail = detail.replaceAll(function_name, function_detail);
//				}
////				×	÷	＋	－	（	）
//				detail = detail.replaceAll("＋", "+");
//				detail = detail.replaceAll("－", "-");
//				detail = detail.replaceAll("×", "*");
//				detail = detail.replaceAll("÷", "/");
//				detail = detail.replaceAll("（", "(");
//				detail = detail.replaceAll("）", ")");
//				System.out.println(name+"  -------->  "+detail);
//				return detail;
//			}
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}		
//		return null;
//	}

	
//	public String convertToFunctionName(String detail) {// function ---->>chinese name  
////		×	÷	＋	－	（	）
//		System.out.println("a+b".replaceAll("a\\+b", "x"));
//		try{
//			if(detail != null){
//				String name = detail;
//				Connection conn = getConnect();
//				ResultSet rs = queryData(conn,"select * from APP_FUNCTION");
//				while(rs.next()){					
//					String function_detail = rs.getString("function_detail");
//					String function_name = rs.getString("function_name");
//					function_detail = function_detail.replaceAll("\\+", "\\\\+");
//					function_detail = function_detail.replaceAll("\\*", "\\\\*");
//					function_detail = function_detail.replaceAll("\\/", "\\\\/");
//					function_detail = function_detail.replaceAll("\\-", "\\\\-");
//					function_detail = function_detail.replaceAll("\\(", "\\\\(");
//					function_detail = function_detail.replaceAll("\\)", "\\\\)");
//					name = name.replaceAll(function_detail,function_name);
//				}
//				name = name.replaceAll( "\\+","＋");
//				name = name.replaceAll( "-","－");
//				name = name.replaceAll( "\\*","×");
//				name = name.replaceAll( "/","÷");
//				name = name.replaceAll( "\\(","（");
//				name = name.replaceAll( "\\)","）");
//				System.out.println(detail+"  -------->  "+name);
//				return name;
//			}
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}		
//		return null;
//	}
	
	public List<FunctionVo> getAllFunction() {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from app_function order by function_name");
			List<FunctionVo> l = new ArrayList<FunctionVo>();
			while(rs.next()){
				FunctionVo fv = new FunctionVo();
				fv.function_detail = rs.getString("function_detail");
				fv.function_name = rs.getString("function_name");
				l.add(fv);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<FunctionVo> getAllFunction2() {
		// TODO Auto-generated method stub
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from APP_FUNCTION2 order by id");
			List<FunctionVo> l = new ArrayList<FunctionVo>();
			while(rs.next()){
				FunctionVo fv = new FunctionVo();
				fv.function_detail = rs.getString("function_detail");
				fv.function_name = rs.getString("function_name");
				l.add(fv);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public String formulaTest(String str,int organization_id,int duty_id, String date) {
		String sql = "select "+str.replaceAll("@organization_id", organization_id+"")
				.replaceAll("@stat_time", "'"+date+"'")
				.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
				.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
				.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
				.replaceAll("@duty_id",duty_id+"").replaceAll("\'\'", "'");
		System.out.println("函数："+sql);
		
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				System.out.println("结果："+rs.getBigDecimal(1));
				String rtStr = rs.getBigDecimal(1)+"";
				freeConnection(conn);
				return rtStr;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<EmployeeVo> formulaTest2(String testFormula,int dispense_id, String stat_time) {	
		List<EmployeeVo> empList = new ArrayList<EmployeeVo>();
		try{
			Connection conn = getConnect();
			String testFormulaRt = "";
			if(testFormula.indexOf(GlobalProperties.GET_DispenseBonusFormula) >= 0){
				String sql = "select related_organization_id from t_sys_stat_organization_vs_dispense_organization where id = "+dispense_id;
				ResultSet rs1 = queryData(conn, sql);
				if(rs1.next()){
					String newRelatedId = rs1.getString(1).replaceAll("::", ":");
					String[] s= newRelatedId.split(":");
					if(s.length <= 0){
						return null;
					}
					String sql1 = "select dbo.GetFullOrganizationName(a.id) as fName," +
							"	a.base_bonus_function_detail as new_function," +
							"	a.duty_id" +
							"	from t_sys_stat_department  as a" +
							"	where a.id = ";
					BigDecimal bt = new BigDecimal("0");
					String totalFormula = "";
					for(int i =0;i<s.length;i++){
						if(s[i].equals("")){
							continue;
						}
						OrganizationFirstBonusVo ovo = new OrganizationFirstBonusVo();
						ovo.organization_id = Integer.valueOf(s[i]);
						ResultSet rs = queryData(conn, sql1+ovo.organization_id);
						if(rs.next()){
							ovo.full_name = rs.getString("fName");
							ovo.new_function = rs.getString("new_function");
							if(ovo.new_function != null && !ovo.new_function.equals("")){
								totalFormula += ovo.new_function+"+";
							}					
							ovo.duty_id = rs.getInt("duty_id");					
						}
						ovo.stat_time = DateTools.stdJustDate(stat_time);
						OrganizationFirstBonusVo rtVo = recalByNewFunction(conn,ovo);
						if(rtVo == null){
							System.out.println(ovo.full_name+"   "+ovo.organization_id+"    has no function ");
							continue;
						}
						String rtValue = rtVo.cal_value;
						if(rtValue != null && !rtValue.equals("")){
							System.out.println(ovo.full_name+"   "+ovo.organization_id+"    "+rtValue);
							bt = bt.add(new BigDecimal(rtValue));
						}else{
							System.out.println(ovo.full_name+"   "+ovo.organization_id+"    NULL?");
							bt = bt.add(new BigDecimal("0"));
						}
					}
					System.out.println("total = "+bt.toString());
					testFormulaRt = testFormula
						.replaceAll(GlobalProperties.GET_DispenseBonusFormula,bt.toString())
						.replaceAll("@dispense_id", dispense_id+"")
						.replaceAll("@stat_time", "'"+stat_time+"'")
						.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
						.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
						.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
						.replaceAll("\'\'", "'");
				}
			}else{
				testFormulaRt = testFormula
				.replaceAll("@dispense_id", dispense_id+"")
				.replaceAll("@stat_time", "'"+stat_time+"'")
				.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
				.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
				.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
				.replaceAll("\'\'", "'");	
			}
			
			System.out.println("科室函数："+testFormulaRt);

			String sql = "select * from t_stat_employee_month " +
					"	where gongzuojixiaohesuandanyuan = " +dispense_id+
					"	and stat_time ='"+stat_time+"'";
			ResultSet rs2 = queryData(conn, sql);
			EmployeeVo evo;
			while(rs2.next()){
				int employee_id = rs2.getInt("employee_id");
				evo = new EmployeeVo();
				evo.id = employee_id;
				evo.valid = rs2.getInt("valid");
				evo.name = rs2.getString("name");
				evo.work_id = rs2.getString("work_id");
				evo.cal_jingyingjixiao = rs2.getBigDecimal("cal_jingyingjixiao")!=null?rs2.getBigDecimal("cal_jingyingjixiao").toString():"0";
				String empFormula = testFormulaRt.replaceAll("@employee_id", employee_id+"")
							.replaceAll("@dispense_id", dispense_id+"")
							.replaceAll("@stat_time", "'"+stat_time+"'")
							.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
							.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
							.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
							.replaceAll("\'\'","\'");
				ResultSet rs3 = queryData(conn, "select "+empFormula);
				if(rs3.next()){
					BigDecimal cal_employee = rs3.getBigDecimal(1);
					evo.cal_gongzuojixiao = cal_employee!=null?cal_employee.toString():"0";
				}else{
					evo.cal_gongzuojixiao = "0";
				} 
				empList.add(evo);
			}						
			freeConnection(conn);
			return empList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	private OrganizationFirstBonusVo recalByNewFunction(
			Connection conn,OrganizationFirstBonusVo selVo) {//如果有KPI 要查时间，然后update之前的计算值，并且把差值计入当月
		try{
			String base_bonus_function_detail = selVo.new_function;
			if(base_bonus_function_detail != null && !base_bonus_function_detail.equals("")){
				if(base_bonus_function_detail != null && !base_bonus_function_detail.equals("")){
					base_bonus_function_detail = base_bonus_function_detail.replaceAll("@organization_id", selVo.organization_id+"");
					base_bonus_function_detail = base_bonus_function_detail.replaceAll("@stat_time", "'"+DateTools.dtsJustDate(selVo.stat_time)+"'");
					base_bonus_function_detail = base_bonus_function_detail.replaceAll("@duty_id", selVo.duty_id+"");
					try{
						String calSTR = "select "+base_bonus_function_detail+" as x";
						calSTR = calSTR.replaceAll("\\[", "(").replaceAll("\\]", ")")
							.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
							.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/");
						ResultSet rs = queryData(conn,calSTR);
						if(rs.next()){
							BigDecimal cal_bonus_ob = rs.getBigDecimal("x");
							if(cal_bonus_ob != null){
								selVo.cal_value = cal_bonus_ob.setScale(4, BigDecimal.ROUND_HALF_UP).toString();
								selVo.old_function = selVo.new_function;	
								return selVo;
							}
						}
					}catch(Exception ex2){
						ex2.printStackTrace();
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public List<FunctionVo> getAllFN() {
		String sql = "select * from sysObjects where type = 'FN'and name like 'FUN_%'";
		try{
			Connection conn = getConnect();
			List<FunctionVo> l = new ArrayList();
			ResultSet rs = queryData(conn,sql);
			while(rs.next()){
				FunctionVo vo = new FunctionVo();
				vo.function_name = rs.getString("name");
				vo.function_id = rs.getInt("id");
				l.add(vo);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}


	

	public String updateAndGetFN(String sql) {
		try{
			sql = sql.replaceAll("\r\r", "\r").replaceAll("\t\t", "\t").replaceAll("\r\t", "\r");
			System.out.println("........................................"+sql);
			Connection conn = getConnect();
			int x = updateData(conn,sql);
			System.out.println(x);
			freeConnection(conn);
			if(x == 1){
				return sql;
			}else{
				return null;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return sql;
	}


	public FunctionVo getFNDetail(FunctionVo vo) {
		String sql = "select * from sys.all_sql_modules " +
				"where object_id = "+vo.function_id;
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				vo.function_definition = rs.getString("definition");				
				if(vo.function_definition != null){
					vo.function_definition = vo.function_definition.replaceAll("\r\r", "\r").replaceFirst("create", "ALTER").replaceFirst("CREATE", "ALTER");
					System.out.println(vo.function_definition);
				}
				freeConnection(conn);
				return vo;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}


	public List<EmployeeVo> formulaTest2DispenseRecent(DispenseOrganizationVo dVo) {
		
		try{
//			Connection conn = getConnect();
//			String testFormulaRt = "";
//			BigDecimal totalBonus = new BigDecimal(0);
//			if(dVo.bonus_str != null){
//				totalBonus =  new BigDecimal(dVo.bonus_str);
//			}
//			
//			System.out.println(">>>>>>>>>>>>:  "+dVo.formula);
//			testFormulaRt = "select "+dVo.formula
//			.replaceAll(GlobalProperties.GET_DispenseBonusFormula,dVo.bonus_str)
//			.replaceAll("@dispense_id", dVo.id+"")
//			.replaceAll("@stat_time", "'"+dVo.stat_time_str+"'")
//			.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
//			.replaceAll("\'\'", "'");
//			
//			System.out.println("科室函数："+testFormulaRt);
//			
//			String sql3 = "select * from t_stat_employee_vs_dispense_organization_month,t_sys_stat_employee," +
//					"	t_stat_employee_vs_post_month " +
//					"	where t_stat_employee_vs_dispense_organization_month.employee_id = t_sys_stat_employee.employee_id" +
//					"	and t_stat_employee_vs_dispense_organization_month.stat_time = t_stat_employee_vs_post_month.stat_time" +
//					"	and t_stat_employee_vs_dispense_organization_month.employee_id = t_stat_employee_vs_post_month.employee_id" +
//					"	and t_stat_employee_vs_dispense_organization_month.dispense_organization_id = t_stat_employee_vs_post_month.dispense_organization_id" +
//					"	and t_stat_employee_vs_dispense_organization_month.dispense_organization_id = "+dVo.id+
//					"	and t_stat_employee_vs_dispense_organization_month.stat_time = '"+dVo.stat_time_str+"'";
//			ResultSet rs = queryData(conn,sql3);
//			List<EmployeeVo> empList = new ArrayList<EmployeeVo>();
//			while(rs.next()){
//				EmployeeVo evo = new EmployeeVo();
//				evo.id = rs.getInt("employee_id");
//				evo.name = rs.getString("employee_name");
//				evo.dispense_bonus_str = totalBonus.toString();
//				evo.dispense_id = dVo.id;
//				evo.formula = testFormulaRt.replaceAll("@employee_id", evo.id+"");
//				evo.work_id = rs.getString("work_id");
//				//等着修改
////				evo.post_name = rs.getString("post_name");
////				evo.post_point = rs.getBigDecimal("point").toString();
////				
////				System.out.println("个人函数： "+evo.formula);
////				
////				ResultSet rs3 = queryData(conn, evo.formula);
////				if(rs3.next()){
////					evo.bonus = rs3.getBigDecimal(1).toString();
////				}
//				empList.add(evo);
//			}
//			freeConnection(conn);
//			return empList;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}


	public List<FunctionVo> getAllFunctionOperate() {
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select * from APP_FUNCTION_OPERATE");
			List<FunctionVo> l = new ArrayList<FunctionVo>();
			while(rs.next()){
				FunctionVo fv = new FunctionVo();
				fv.function_detail = rs.getString("function_detail");
				fv.function_name = rs.getString("function_name");
				l.add(fv);
			}
			freeConnection(conn);
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}


	public String formulaTestOperate(String str, int operate_id, String date) {

		String sql = "select "+str.replaceAll("@operate_id", operate_id+"")
				.replaceAll("@stat_time", "'"+date+"'")
				.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
				.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
				.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
				.replaceAll("\'\'", "'");
		System.out.println("函数："+sql);
		
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				System.out.println("结果："+rs.getBigDecimal(1));
				String rtStr = rs.getBigDecimal(1)+"";
				freeConnection(conn);
				return rtStr;
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}


	public List<EmployeeVo> getAndUpdateEmpGongzuojixiao(
			DispenseOrganizationVo vo) {
//		try{
//			Connection conn = getConnect();
//			String sql = "select * from t_stat_employee_month " +
//					"	where gongzuojixiaohesuandanyuan = " +vo.id+
//					"	and stat_time ='"+vo.stat_time_str+"'" +
//					"	order by name";
//			ResultSet rs = queryData(conn,sql);
//			List<EmployeeVo> l = new ArrayList();
//			EmployeeVo evo ;
//			while(rs.next()){
//				evo = new EmployeeVo();
//				evo.cal_gongzuojixiao = 
//			}
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
		return null;
	}

	

	public String getFunctionChinese(String str) {
		try{
			if(str == null || "".equals(str)){
				return "";
			}
			Connection conn = getConnect();
			String sql = "select id,function_detail as fn,function_name as name from APP_FUNCTION" +
			"	UNION ALL" +
			"	select id,function_detail as fn,function_name as name from APP_FUNCTION_OPERATE" +
			"	UNION ALL" +
			"	select id,function_detail as fn,function_name as name from APP_FUNCTION2" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT2" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT_ADMIN" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT_OPERATE";
			List<String[]> l = new ArrayList();
			ResultSet rs = queryData(conn,sql);
			while(rs.next()){
				String s[] = new String[3];
				s[0] = rs.getInt("id")+"";
				s[1] = rs.getString("fn")+"";
				s[2] = rs.getString("name")+"";
				l.add(s);
			}
			StringBuffer tmpSB = new StringBuffer();
			StringBuffer wordSB = new StringBuffer();
			String nowStr = str.replaceAll(" ", "");
			for(int i = 0;i < nowStr.length(); i++){
				String x = nowStr.charAt(i)+"";
				if(x.equals("[") || x.equals("]") || 
					x.equals("＋") || x.equals("－") || 
					x.equals("×") || x.equals("÷") ){
					wordSB.append(getFunctionName(tmpSB.toString(),l));
					wordSB.append(x);					
					tmpSB = new StringBuffer();
				}else if(i == nowStr.length()-1){
					tmpSB.append(x);
					wordSB.append(getFunctionName(tmpSB.toString(),l));
				}else{
					tmpSB.append(x);
				}
			}
			System.out.println("+++++:  "+wordSB.toString());
			return wordSB.toString();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	private String getFunctionName(String name,List<String[]> l){
		System.out.println("::::  "+name);
		if(name.equals("")){
			return "";
		}
		if(name.equals("GET_DispenseBonusFormula")){
			return "科室奖金";
		}
		if(name.indexOf("dbo.UP_Period_") == 0 && name.indexOf(")") == (name.length()-1)){
			return "项目点数";
		}
		if(name.indexOf("dbo.KPI_Period") == 0 && name.indexOf(")") == (name.length()-1)){
			return "KPI分数";
		}
		if(name.indexOf("dbo.COST_Period") == 0 && name.indexOf(")") == (name.length()-1)){
			return "成本核算得分";
		}	
		
		
		if(name.indexOf("dbo.FUN_VOTE(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE(") && s[0].equals(num)){
					return s[2];
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE2(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[5].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE2(") && s[0].equals(num)){
					return s[2];
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE_OPERATE(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE_OPERATE(") && s[0].equals(num)){
					return s[2];
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE_ADMIN(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE_ADMIN(") && s[0].equals(num)){
					return s[2];
				}
			}
		}
		if(name.indexOf(")") == (name.length()-1)){//
			String nm = name.split("\\(")[0];
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith(nm)){
					return s[2];
				}
			}
		}
		return name;
	}
	public static void main(String s[]){
		String x = "dbo.FUN_VOTE(@organization_id,@stat_time,@start_time,@end_time,14)";
		System.out.println(x.indexOf("dbo.FUN_VOTE("));
		System.out.println(x.lastIndexOf("("));
		System.out.println(x.split(",").length);
		System.out.println(x.split("\\(").length);
	}


	public String getFunctionChineseAndDetail(String str) {
		try{
			if(str == null || str.equals("")){
				return "";
			}
			Connection conn = getConnect();
			String sql = "select id,function_detail as fn,function_name as name from APP_FUNCTION" +
			"	UNION ALL" +
			"	select id,function_detail as fn,function_name as name from APP_FUNCTION_OPERATE" +
			"	UNION ALL" +
			"	select id,function_detail as fn,function_name as name from APP_FUNCTION2" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT2" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT_ADMIN" +
			"	UNION ALL" +
			"	select id,function_detail as fn,name from VOTE_DICT_OPERATE";
			List<String[]> l = new ArrayList();
			ResultSet rs = queryData(conn,sql);
			while(rs.next()){
				String s[] = new String[3];
				s[0] = rs.getInt("id")+"";
				s[1] = rs.getString("fn")+"";
				s[2] = rs.getString("name")+"";
				l.add(s);
			}
			StringBuffer tmpSB = new StringBuffer();
			StringBuffer wordSB = new StringBuffer();
			String nowStr = str.replaceAll(" ", "");
			int nb = 0;
			for(int i = 0;i < nowStr.length(); i++){
				String x = nowStr.charAt(i)+"";
				if(x.equals("[") || x.equals("]") || 
					x.equals("＋") || x.equals("－") || 
					x.equals("×") || x.equals("÷") ){
					String wd = getFunctionName2(tmpSB.toString(),l);
					if(!"".equals(wd)){
						wd = wd+"##";
						nb++;
					}
					wordSB.append(wd);					
					nb++;
					if(i != nowStr.length()-1)
					{
						wordSB.append(x).append("@@"+nb).append("##");
					}
					else
					{
						wordSB.append(x).append("@@"+nb);
					}					
					tmpSB = new StringBuffer();
				}else if(i == nowStr.length()-1){
					tmpSB.append(x);
					String wd = getFunctionName2(tmpSB.toString(),l);
					if(!"".equals(wd)){
						nb++;
						wordSB.append(wd);
					}
					
				}else{
					tmpSB.append(x);
				}
			}
			System.out.println("DETAIL-----:  "+wordSB.toString());
			return wordSB.toString();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	
	}
	private String getFunctionName2(String name,List<String[]> l){
//		System.out.println("::::  "+name);
		
		if(name.equals("")){
			return "";
		}
		name = name.replaceAll("'", "");
		if(name.equals("GET_DispenseBonusFormula")){
			return "科室奖金"+"@@"+name;
		}
		if(name.indexOf("dbo.UP_Period_") == 0 && name.indexOf(")") == (name.length()-1)){			   
			return "项目点数("+name.split(",")[3]+" - "+name.split(",")[4].replaceAll("\\)", "")+")@@"+name;
		}
		if(name.indexOf("dbo.KPI_Period") == 0 && name.indexOf(")") == (name.length()-1)){
			return "KPI分数("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
		}
		if(name.indexOf("dbo.COST_Period") == 0 && name.indexOf(")") == (name.length()-1)){
			return "成本核算得分"+"@@"+name;
		}	
		
		
		if(name.indexOf("dbo.FUN_VOTE(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE(") && s[0].equals(num)){
					return s[2]+"("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE2(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[5].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE2(") && s[0].equals(num)){
					return s[2]+"("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE_OPERATE(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE_OPERATE(") && s[0].equals(num)){
					return s[2]+"("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
				}
			}
		}
		if(name.indexOf("dbo.FUN_VOTE_ADMIN(") == 0 && name.indexOf(")") == (name.length()-1)){//
			String num = name.split(",")[4].replaceAll("\\)", "");
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith("dbo.FUN_VOTE_ADMIN(") && s[0].equals(num)){
					return s[2]+"("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
				}
			}
		}
		if(name.indexOf(")") == (name.length()-1)){//
			String nm = name.split("\\(")[0]+"(";
			for(int i = 0;i<l.size();i++){
				String s[] = l.get(i);
				if(s[1].startsWith(nm)){
					return s[2]+"("+name.split(",")[2]+" - "+name.split(",")[3].replaceAll("\\)", "")+")@@"+name;
				}
			}
		}
		return name+"@@"+name;
	}
	
	
	public List<DataTableVo> checkDate(int i,String dateS)
	{
		try{
			Connection conn = getConnect();
			ResultSet rs = queryData(conn,"select id,(select name from dict_duty_type where id = duty_id)+'-'+name as nm,duty_id " +
					" from t_sys_stat_department order by nm");
			List<DataTableVo> l = new ArrayList();
			DataTableVo s ;
			while(rs.next()){
				int id = rs.getInt(1);
				String name = rs.getString(2);
				int duty_id = rs.getInt(3);
				String sql = "";
				if(i == 1)
				{
					sql = "select '点数',dbo.UP_Period_"+id+"("+id+","+duty_id+",'"+dateS+"','0','1')";
				}
				else if(i == 2)
				{
					sql = "select '门诊人次',dbo.FUN_MenZhenRenCi("+id+",'"+dateS+"','0','1')";
				}else if(i == 3)
				{
					sql = "select '出院人次',dbo.FUN_ChuYuanRenCi("+id+",'"+dateS+"','0','1')";
				}
				try
				{
					ResultSet rs2 = queryData(conn,sql);
					while(rs2.next())
					{
						s = new DataTableVo();
						s.name= name;
						s.type = rs2.getString(1);
						s.value = rs2.getBigDecimal(2).toString();
						System.out.println(s.name+"  "+s.type+"  "+s.value);
						l.add(s);
					}
				}
				catch(Exception ex)
				{
					ex.printStackTrace();
				}
				
			}
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
